﻿/* util script start*/
function getid(element) {
  if (typeof element == 'string')
   return document.getElementById(element);
}
function GetGroupValue(groupName){
	var group=document.getElementsByName(groupName);
	if(!group)
		return null;
	for(var i=0;i<group.length;i++){
		if(group[i].checked)
			return group[i].value;
	}
	return null;
}
/*
hide=function(element) {
  getid(element).style.display = 'none';
  return element;
}

show=function(element) {
  getid(element).style.display = '';
  return element;
}
*/
Date.prototype.format = function(format)  
{  
   var o = {  
     "M+" : this.getMonth()+1, //month  
     "d+" : this.getDate(),    //day  
     "h+" : this.getHours(),   //hour  
     "m+" : this.getMinutes(), //minute  
     "s+" : this.getSeconds(), //second  
     "q+" : Math.floor((this.getMonth()+3)/3), //quarter  
     "S" : this.getMilliseconds() //millisecond  
   }  
   if(/(y+)/.test(format)) format=format.replace(RegExp.$1,  
     (this.getFullYear()+"").substr(4 - RegExp.$1.length));  
   for(var k in o)if(new RegExp("("+ k +")").test(format))  
     format = format.replace(RegExp.$1,  
       RegExp.$1.length==1 ? o[k] :   
         ("00"+ o[k]).substr((""+ o[k]).length));  
   return format;  
} 
/* util script end*/

function SetOrder(){
	ShowPage(currentpage);
}

function GetPageCount(fun){
	db.GetTotalCount(function(result){
						var itemcount = result;
						if(itemcount<=0)
						{
							fun(0);
						}
						var flr = Math.floor(itemcount/PAGE_SIZE);
						if(0==itemcount%PAGE_SIZE)
						{
							flr -= 1;
						}
						fun(flr);
					}
	)
}

function RemoveWord(id){
	//Debug("Remove:id:"+id);
	var p=document.getElementById("div_words");
	var div=document.getElementById(id);
	p.removeChild(div);
	db.DeleteArticle(div.idArticle);
	ShowPage(currentpage);
}
function ListTags()
{
  getid("div_tags").innerHTML="";
  db.GetTags(function(rs){
      //Debug('GetTags');
  		if(!rs)
  			return;
      var div = document.createElement("div");
      div.innerHTML = rs.name;
      div.tag_id = rs.id;
      div.style = "tagdiv";
      getid("div_tags").appendChild(div);	
    }
  )
}

function ListWordPageList(pageIndex){
	var order = "order by [addTime] DESC";
	//var order = "order by [GVER] DESC";

	document.getElementById("div_words").innerHTML = "";
	db.GetRows(pageIndex,PAGE_SIZE,order,function(rs){
						//Debug('add');
						if(!rs)
							return;
						//var reg = new RegExp(rs.title,"gim");	
						var div = document.createElement("div");
						var sourceDiv='';
						var sen='';
						if(rs.pageUrl && rs.pageUrl.indexOf('http')==0)
							sourceDiv='<div class="opBtn"><a href="'+rs.pageUrl+'" title="' + rs.pageUrl + '" target="_blank"><image src="go.png"></a></div>';
						sen=rs.body;
						div.id = "div_newword_" + rs.id;
						div.idArticle = rs.id;
						div.className="lineBlock";
						div.innerHTML='<div class="wordTitle">'
											+'<div class="keyWord">'+rs.title+'</div>'
											+sourceDiv
											+'<div class="opBtn"><a title="删除" onclick=\'RemoveWord("'+div.id+'")\' href="#"><image src="del.png"></a></div>'
											+'<div class="opBtn">'+new Date(rs.addTime).format("yyyy/MM/dd hh:mm")+'</div>'
										+'</div>'
										+'<div style="font-size:13px;"><pre>'+sen+'</pre></div>';
						getid("div_words").appendChild(div);	
						//var arr=div.getElementsByTagName("div");
						//if(arr.length>0){
						//	arr[arr.length-1].onclick();
						//}
					}
				);
}

function ShowPage(i){
	GetPageCount(function(result){
					//Debug(result);
					if(result<i)
						return;
					document.getElementById("itemcount").innerHTML = (result+1) + "页之" + (i+1);
					ListWordPageList(i);
				}
	)
}

function ShowPrevPage(){
	if(currentpage>0){
		currentpage--;
		ShowPage(currentpage);
	}
}

function ShowNextPage(){
	GetPageCount(function(result){
		var nextpage=currentpage+1;
		if(nextpage<=result){
			currentpage++;
			ShowPage(currentpage);
		}
		}
	);	
}

function Debug(str){
	var s=getid("debug").innerHTML;
	getid("debug").innerHTML=s+"<br>"+str;
}